using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using AutoMapper;
using SapaFinance.Domain.Model;
using SapaFinance.GenericRepository;

namespace SapaFinance.OOMap.Helpers
{
    public class BankToSelectListConvert : TypeConverter<Bank, IEnumerable<SelectListItem>>
    {
        private readonly IGenericRepository<Bank, Guid> _repository;

        public BankToSelectListConvert(IGenericRepository<Bank, Guid> repository)
        {
            _repository = repository;
        }

        protected override IEnumerable<SelectListItem> ConvertCore(Bank source)
        {
            var agents = _repository.GetAll();
            if (source == null)
            {
                return agents.Select(x => new SelectListItem
                                          {
                                              Value = x.UID.ToString(),
                                              Text = x.Name,
                                          });
            }
            return agents.Select(x => new SelectListItem
                                      {
                                          Value = x.UID.ToString(),
                                          Text = x.Name,
                                          Selected = source.UID == x.UID
                                      });
        }
    }
}